package com.prometheus.aspectj;

import com.prometheus.annotation.CounterMethod;
import com.prometheus.util.Metric;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

/**
 * @author: Alan
 * @date: 2021/5/18 13:18
 */
@Order(value = Ordered.LOWEST_PRECEDENCE - 100)
@Aspect
public class CounterAspectj {

    private static Logger log = LoggerFactory.getLogger(CounterAspectj.class);

    @Around("@annotation(counterMethod)")
    public Object methodInvoke(ProceedingJoinPoint jp, CounterMethod counterMethod) throws Throwable {
        String name = counterMethod.name();

        Object proceed = jp.proceed();
        Metric.counter(name, "").inc();
        return proceed;
    }
}
